package com.zkzx.wlhy.adaptor.service.company;

import com.alibaba.fastjson.JSON;
import com.zkzx.common.company.command.CompanyCommand;
import com.zkzx.common.company.constants.CompanyStatusWlhyEnum;
import com.zkzx.common.company.model.CompanyFinanceVO;
import com.zkzx.common.company.model.CompanyOperationVO;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.constants.SourceEnum;
import com.zkzx.common.exception.BaseException;
import com.zkzx.common.exception.ExceptionEnum;
import com.zkzx.common.global.Global;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.util.date.DateUtil;
import com.zkzx.util.json.JSONResultUtil;
import com.zkzx.wlhy.adaptor.clinet.ResultVO;
import com.zkzx.wlhy.adaptor.clinet.company.CompanyClient;
import com.zkzx.wlhy.adaptor.controller.company.param.CompanyDTO;
import com.zkzx.wlhy.adaptor.controller.company.vo.WlhyCompanyVO;
import com.zkzx.wlhy.adaptor.controller.company.vo.WlhyContractVO;
import com.zkzx.wlhy.adaptor.service.code.WlhyCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/19/19:25
 * @Description:
 */
@Slf4j
@Service
public class CompanyService {

    @Autowired
    private CompanyClient companyClient;


    public CompanyCommand getCompany(Integer id, String name, String code){

        ResultVO<WlhyCompanyVO> resultVO = companyClient.getCompany(name, code);
        WlhyCompanyVO wlhyCompanyVO = resultVO.getData();

        CompanyCommand companyCommand = new CompanyCommand();
        companyCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.NO_EXIST.name())
                    .build());
            companyCommand.setSource(SourceEnum.ONESELF.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            buildCompanyCommand(companyCommand,wlhyCompanyVO);

        }else {
            throw new BaseException(ExceptionEnum.WH_DATA_ERROR.getCode(),ExceptionEnum.WH_DATA_ERROR.getMsg());
        }

        return companyCommand;
    }


    public CompanyCommand add(Integer id, CompanyVO companyVO) {
        CompanyDTO companyDTO = new CompanyDTO();
        companyDTO.setCompanyName(companyVO.getName());
        companyDTO.setCreditCode(companyVO.getCode());
        List<Integer> list = new ArrayList<>();
        list.add(Global.ONE);
        companyDTO.setIncludeBusinessType(list);
        companyDTO.setType(Global.ONE);
        companyDTO.setEnterpriseType(companyVO.getType());
        companyDTO.setBankAccount(companyVO.getCompanyFinance().getBankAmount());
        companyDTO.setBankName(companyVO.getCompanyFinance().getBank());
        companyDTO.setBusinessLicense(companyVO.getBusinessPicURL());
        companyDTO.setEstablishDate(DateUtil.convertStringDateToStringDateTime(companyVO.getFoundTime()));
        companyDTO.setRegisteredCapital(companyVO.getRegAmount());
        companyDTO.setRegisteredDate(DateUtil.convertStringDateToStringDateTime(companyVO.getFoundTime()));
        companyDTO.setRegistrationProvince(companyVO.getProvince());
        companyDTO.setRegistrationCity(companyVO.getCity());
        companyDTO.setRegistrationCounty(companyVO.getCounty());
        companyDTO.setRegistrationDetailAddress(companyVO.getRegAddress());
        companyDTO.setTaxationAuthorities(companyVO.getTaxAuthorities());

        ResultVO resultVO = companyClient.add(companyDTO);
        //提交完，改状态，更新第三方企业id
        CompanyCommand companyCommand = new CompanyCommand();
        companyCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            companyCommand.setThreeId(String.valueOf(resultVO.getData()));
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.WAIT.name())
                    .build());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            WlhyCompanyVO wlhyCompanyVO = JSON.parseObject(JSON.toJSONString(resultVO.getData()), WlhyCompanyVO.class);
            buildCompanyCommand(companyCommand,wlhyCompanyVO);
        }else {
            throw new BaseException(ExceptionEnum.WH_DATA_ERROR.getCode(),ExceptionEnum.WH_DATA_ERROR.getMsg());
        }
        return companyCommand;
    }


    public CompanyCommand getStatus(Integer id,String threeId) {
        ResultVO<String> resultVO = companyClient.getStatus(threeId);

        //提交完，改状态，更新第三方企业id
        CompanyCommand companyCommand = new CompanyCommand();
        companyCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.REVIEW.name())
                    .build());
        }else if (WlhyCode.FOUR_HUNDRED.equals(resultVO.getCode())){
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.NO_REVIEW.name())
                    .build());
        }else if (WlhyCode.FOUR_HUNDRED_AND_ONE.equals(resultVO.getCode())){
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.NO_REVIEW.name())
                    .build());
        }else if (WlhyCode.TWO_HUNDRED_AND_FIVE.equals(resultVO.getCode())){
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.WAIT.name())
                    .build());
        }else {
            companyCommand.setStatusVO(StatusVO.builder()
                    .value(CompanyStatusWlhyEnum.NO_REVIEW.name())
                    .build());
        }

        return companyCommand;
    }

    public CompanyCommand getContract(Integer id, CompanyVO companyVO) {
        WlhyContractVO wlhyContractVO = new WlhyContractVO();
        wlhyContractVO.setCompanyId(companyVO.getThreeId());
        wlhyContractVO.setClientCreditCode(companyVO.getCode());
        wlhyContractVO.setContractBeginTime(DateUtil.convertStringDateToStringDateTime(companyVO.getContractStartTime()));
        String contractEndTime = StringUtils.isEmpty(companyVO.getContractEndTime())
                ? Global.TWO_THOUSAND_AND_NINETY_NINE_YYYYMMDD_HHMMSS
                : DateUtil.convertStringDateToStringDateTime(companyVO.getContractEndTime());
        wlhyContractVO.setContractEndTime(contractEndTime);
        wlhyContractVO.setContractFile(companyVO.getContractFileURL());
        wlhyContractVO.setContractNum(companyVO.getContractCode());

        ResultVO<String> resultVO = companyClient.getContract(wlhyContractVO);

        //提交完，改状态，更新第三方企业id
        CompanyCommand companyCommand = new CompanyCommand();

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            companyCommand.setId(id);
            companyCommand.setContractCode(resultVO.getData());
        }else{
            companyCommand.setId(id);
            companyCommand.setRemark(resultVO.getMessage());
        }

        return companyCommand;
    }


    private void buildCompanyCommand(CompanyCommand companyCommand, WlhyCompanyVO wlhyCompanyVO){
        companyCommand.setStatusVO(StatusVO.builder()
                .value(CompanyStatusWlhyEnum.REVIEW.name())
                .build());

        companyCommand.setEmail(wlhyCompanyVO.getContactEmail());
        companyCommand.setThreeId(wlhyCompanyVO.getId());
        companyCommand.setName(wlhyCompanyVO.getName());
        companyCommand.setType(wlhyCompanyVO.getEnterpriseType());
        companyCommand.setPerson(wlhyCompanyVO.getLegalPerson());
        companyCommand.setRegionInfo(wlhyCompanyVO.getRegistrationDetailAddress());
        companyCommand.setProvince(wlhyCompanyVO.getRegistrationProvince());
        companyCommand.setCity(wlhyCompanyVO.getRegistrationCity());
        companyCommand.setCounty(wlhyCompanyVO.getRegistrationCounty());
        companyCommand.setRegAddress(wlhyCompanyVO.getRegistrationDetailAddress());
        companyCommand.setRegTime(wlhyCompanyVO.getRegisteredDate());
        companyCommand.setFoundTime(wlhyCompanyVO.getEstablishDate());
        companyCommand.setTaxAuthorities(wlhyCompanyVO.getTaxationAuthorities());
        companyCommand.setRegAmount(wlhyCompanyVO.getRegisteredCapital());
        companyCommand.setBusinessPicURL(wlhyCompanyVO.getBusinessLicense());
        companyCommand.setLicensePicURL(wlhyCompanyVO.getOpenPermit());
        companyCommand.setAuthorizePicURL(wlhyCompanyVO.getEnterpriseAuthorization());
        companyCommand.setMemberMobile(wlhyCompanyVO.getUserphone());
        companyCommand.setSource(SourceEnum.SHARE.name());

        // 更新活动信息
        List<WlhyCompanyVO.Contracts> contracts = wlhyCompanyVO.getContracts();
        if(CollectionUtils.isNotEmpty(contracts)){
            WlhyCompanyVO.Contracts contract = contracts.get(Global.ZERO);
            companyCommand.setContractCode(contract.getId());
            companyCommand.setContractStartTime(contract.getContractBeginTime());
            companyCommand.setContractEndTime(contract.getContractEndTime());
            companyCommand.setContractFileURL(contract.getContractFile());
        }

        CompanyFinanceVO companyFinanceVO = new CompanyFinanceVO();
        companyFinanceVO.setBank(wlhyCompanyVO.getBankName());
        companyFinanceVO.setBankAmount(wlhyCompanyVO.getBankAccount());


        CompanyOperationVO companyOperationVO = new CompanyOperationVO();
        companyOperationVO.setAddress(wlhyCompanyVO.getContactDetailAddress());
        companyOperationVO.setName(wlhyCompanyVO.getContactName());
        companyOperationVO.setMobile(wlhyCompanyVO.getContactTelephone());
        companyOperationVO.setProvince(wlhyCompanyVO.getContactProvince());
        companyOperationVO.setCity(wlhyCompanyVO.getContactCity());
        companyOperationVO.setCounty(wlhyCompanyVO.getContactCounty());

        companyCommand.setCompanyFinance(companyFinanceVO);
        companyCommand.setCompanyOperation(companyOperationVO);
    }

}
